<?php
declare(strict_types=1);

namespace hangpu8\hpcms\controller;

use hangpu8\hpcms\Utils\Json;
use think\exception\ValidateException;

class AdminRole extends Base
{
    /**
     * 列表
     *
     * @return void
     */
    public function index()
    {
        $AdminRoleService = new \hangpu8\hpcms\service\AdminRole;
        $list = $AdminRoleService->getlist();
        $builder = new \hangpu8\hpcms\Utils\Builder\ListBuilder;
        $data = $builder
            ->addColumn('rightButtonList', '操作', [
                'type'              => 'template',
                'template'          => 'rightButtonList'
            ])
            ->addTopButton('add', '添加', [
                'title'             => '添加数据',
                'api'               => 'hangpu8/AdminRole/add',
            ])
            ->addRightButton('auth', '授权',[
                'api'               => 'hangpu8/AdminRole/auth',
                'title'             => '授予角色权限',
            ])
            ->addRightButton('edit', '修改',[
                'api'               => 'hangpu8/AdminRole/edit',
                'title'             => '修改',
            ])
            ->addRightButton('delete', '删除', [
                'api'               => 'hangpu8/AdminRole/del',
                'method'            => 'delete',
                'title'             => '确认要删除该数据吗？',
                'modalType'         => 'confirm',
                'width'             => '600',
                'okText'            => '确认删除',
                'cancelText'        => '取消操作',
                'content'           => '<p>删除该数据不可恢复</p>',
            ])
            ->addColumn('id', '序号',[
                'width'             => '100',
            ])
            ->addColumn('ctime', '添加时间',[
                'width'             => '200',
            ])
            ->addColumn('title', '角色名称')
            ->addColumn('is_system_text', '是否系统',[
                'width'             => '120',
            ])
            ->setData($list['data'])
            ->create();
        return Json::successful($data);
    }

    /**
     * 添加数据
     *
     * @return void
     */
    public function add()
    {
        $service = new \hangpu8\hpcms\service\AdminRole;
        if (request()->isPost())
        {
            $post = request()->post();
            try {
                validate(\hangpu8\hpcms\validate\AdminRole::class)
                    ->check($post);
            } catch (ValidateException $e) {
                list($code, $msg) = explode("\n", $e->getMessage());
                return Json::fail($msg,(int)$code);
            }
            try {
                $service->create($post);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('post')
            ->addRow('title','input','角色名称')
            ->create();
            return Json::successful($data);
        }
    }

    /**
     * 修改数据
     *
     * @param [type] $path
     * @return void
     */
    public function edit($id)
    {
        $service = new \hangpu8\hpcms\service\AdminRole;
        if (request()->isPut())
        {
            $post = request()->post();
            try {
                validate(\hangpu8\hpcms\validate\AdminRole::class)
                    ->check($post);
            } catch (ValidateException $e) {
                list($code, $msg) = explode("\n", $e->getMessage());
                return Json::fail($msg,(int)$code);
            }
            try {
                $service->saveUpdate($post,['id'=> $id]);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $form = $service->getForm(['id'=> $id]);
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('put')
            ->addRow('title','input','角色名称')
            ->setFormData($form)
            ->create();
            return Json::successful($data);
        }
    }

    /**
     * 删除数据
     *
     * @param [type] $path
     * @return void
     */
    public function del($id)
    {
        $service = new \hangpu8\hpcms\service\AdminRole;
        try {
            $service->del($id);
            return Json::successful([],'删除成功');
        } catch (\Throwable $e) {
            return Json::fail($e->getMessage(),$e->getCode());
        }
    }

    /**
     * 角色授权
     *
     * @param [type] $id
     * @return void
     */
    public function auth($id)
    {
        $service = new \hangpu8\hpcms\service\AdminRole;
        if (request()->isPut())
        {
            $post = request()->post();
            try {
                $data = [];
                if ($post['auth'])
                {
                    $data['rule'] = array_filter($service->getParents($post['auth']));
                }
                $service->saveUpdate($data,['id'=> $id]);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $form = $service->getForm(['id'=> $id]);
            $authDb = new \hangpu8\hpcms\model\AuthRule;
            $rule = $authDb->order('sort asc,ctime asc')->limit(1000)->field('path,title,pid')->select()->toArray();
            $rule = \hangpu8\hpcms\Utils\Data::channelLevel($rule,'','','path','pid');
            $authRule = $service->getAuthRule($rule);
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('put')
            ->addRow('title','input','角色名称','',[
                'disabled'              => true,
            ])
            ->addRow('auth','tree','权限授权',[],[
                'data'                  => $authRule,
                'showCheckbox'          => true,
                'props'                 => [

                ],
            ])
            ->setFormData($form)
            ->create();
            return Json::successful($data);
        }
    }
}